package com.ruoyi.bigScreen;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.zzw.BO.SafenessRankingBO;
import com.ruoyi.common.core.domain.zzw.TRegionInfo;
import com.ruoyi.common.core.domain.zzw.VO.SafenessRankingVO;
import com.ruoyi.system.service.ITRegionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 地区Controller
 *
 * @author xbx
 * @date 2023-10-20
 */

@Api(tags = "地区管理")
@RestController
@RequestMapping("/screen/region")
public class ScreenRegionController extends BaseController {
    @Autowired
    private ITRegionInfoService tRegionInfoService;

    @ApiOperation("获取地区下拉树列表")
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect() {

        List<TreeSelect> selects = new ArrayList<>();
        List<TRegionInfo> regions = tRegionInfoService.list();
        Map<Long, String> map = regions.stream().collect(Collectors.toMap(TRegionInfo::getCRegionId, TRegionInfo::getCAreaCode));
        if (CollectionUtils.isEmpty(regions)) {
            return AjaxResult.success(selects);
        }
        for (TRegionInfo region : regions) {
            if (region.getCFatherRegionId() == 0 ){
                continue;
            }
            TreeSelect select = new TreeSelect();
            select.setId(Long.valueOf(region.getCAreaCode().replace(" ","").replaceAll("\\s", "")));
            select.setLabel(region.getCRegionName());
            Long pCode = region.getCFatherRegionId() == 0 ? null : Long.parseLong(map.get(region.getCFatherRegionId()));
            select.setParentId(pCode);
            selects.add(select);
        }
        List<TreeSelect> selectList = TreeSelect.buildTree(selects);
        return success(selectList);
    }

    @ApiOperation("通过分数排名取")
    @PostMapping("/listSafenessScoreTop10")
    public AjaxResult listSafenessScoreTop10(@RequestBody(required = false) SafenessRankingBO safenessRankingBO) {
        List<SafenessRankingVO> regions = tRegionInfoService.listSafenessScoreTop10(safenessRankingBO);
        return AjaxResult.success(regions);
    }
}
